﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Imaginecup.shareData;
using Imaginecup.Recycling.GameMan;
using Imaginecup.Recycling.Object;
using Imaginecup.recycling.Object.Item;
using Imaginecup.recycling.Object;
using Microsoft.Xna.Framework.Graphics;
using Imaginecup.recycling.Factory;
using Imaginecup.util.input;
using Imaginecup.recycling.GameMan;
using Microsoft.Xna.Framework.Input;
using Imaginecup.util;
using Imaginecup.Engine;
using Microsoft.Xna.Framework.Input.Touch;
using Imaginecup.gameState.recycling.GameMan;

namespace Imaginecup.recycling.Stage
{
    public class Stage1 : BaseStage, IStage, IStageBaseBehavior
    {
        private TouchTrailLayer m_TrailLayer;
        private ItemManager m_InventoryManager;

        public Stage1()
        {
            //사용할 리소스 초기화
            Camera.WorldRectangle = new Rectangle(0, 0, Stage2.MapWidth, Stage2.MapHeight);
            this.Initialize();
        }

        protected override void Initialize()
        {
            m_GameManager = new GameManager();
            //m_Manager.Initialize();

            m_TrailLayer = TouchTrailLayer.GetTouchTrailLayer();
            m_InventoryManager = new ItemManager();//.GetInstance();
            
            IObjectFactory factory = ObjectFactory.GetInstance();

            m_GameManager.AddObject(factory.GetObject(ObjectID.BACKGROUND_BACK));
            m_GameManager.AddObject(factory.GetObject(ObjectID.BACKGROUND));
            m_GameManager.AddObject(factory.GetObject(ObjectID.BACKGROUND_FRONT));

            for (int i = 0; i < 3; i++)
            {
                m_GameManager.AddObject(factory.GetObject(ObjectID.CLOUD));
            }

            m_GameManager.AddObject(factory.GetObject(ObjectID.PLAYER));


            //맵 초기화
            TileMap.Initialize(ResourceManager.GetInstance().GetContent("SpriteSheet"));

#if WINDOWS_PHONE
            m_GameManager.AddObject(factory.GetObject(ObjectID.JUMPBTN));
            m_GameManager.AddObject(factory.GetObject(ObjectID.SLIDINGBTN));
#endif
        }

        public override void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            base.Draw(spriteBatch, gameTime);
            //타일맵 그리기
            TileMap.Draw(spriteBatch);
            m_InventoryManager.Draw(spriteBatch, gameTime);

            m_TrailLayer.Draw();
        }

#if WINDOWS
        private bool m_bMousePress = false;
        public override void Update(GameTime gameTime)
        {
            MouseState currentMouseState = MouseInputHandler.GetInputHandler().MouseState;
            MouseState prevMouseState = MouseInputHandler.GetInputHandler().PreviousMouseState;

            if (prevMouseState.LeftButton == ButtonState.Released && currentMouseState.LeftButton == ButtonState.Pressed)
            {
                m_bMousePress = true;
                m_TrailLayer.TouchBegan(currentMouseState.X, currentMouseState.Y, ResourceManager.GetInstance().GetContent("streak" + RandomUtil.rand.Next(1, 5)), 10);
            }
            else if (prevMouseState.LeftButton == ButtonState.Pressed && currentMouseState.LeftButton == ButtonState.Released)
            {
                m_bMousePress = false;
                m_TrailLayer.TouchEnd();
            }
            else if (m_bMousePress)
            {
                m_TrailLayer.TouchMove(currentMouseState.X, currentMouseState.Y);
            }

            base.Update(gameTime);
            m_InventoryManager.Update(gameTime);
            m_TrailLayer.Update();
        }
#endif
#if XBOX
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            m_InventoryManager.Update(gameTime);
        }
#endif
#if WINDOWS_PHONE
        public override void Update(GameTime gameTime)
        {
            //m_ObjectManager.Update(gameTime);
            IPhoneInputHandler input = PhoneInputHandler.GetInputHandler();
            TouchCollection collection = input.GetCurrentTouch();

            if (collection.Count > 0)
            {
                foreach (TouchLocation touch in collection)
                {
                    if (touch.State == TouchLocationState.Pressed)
                    {
                        m_TrailLayer.TouchBegan(touch, ResourceManager.GetInstance().GetContent("streak" + RandomUtil.rand.Next(1, 5)), 10);
                    }
                    else if (touch.State == TouchLocationState.Moved)
                    {
                        m_TrailLayer.TouchMove(touch);
                    }
                    else if (touch.State == TouchLocationState.Released)
                    {
                        m_TrailLayer.TouchEnd(touch);
                    }
                }
            }
            base.Update(gameTime);

            m_InventoryManager.Update(gameTime);
            m_TrailLayer.Update();
            
        }
#endif
        public void Release()
        {
            m_GameManager.RemoveAll();
        }
    }

}
